﻿using System;
using System.Windows;
using System.Windows.Media.Imaging;
using MangaUtils;
using Microsoft.WindowsAPICodePack.Taskbar;
using System.Windows.Controls;
using System.Collections.Generic;

namespace MangaReader
{
	/// <summary>
	/// Reader itself, based on Mitsu's Book Control.
	/// </summary>
	public partial class Reader
	{
		/// <summary>
		/// Manga to display
		/// </summary>
		private Manga manga;
		/// <summary>
		/// Is readere ready to be drawn onto screen?
		/// </summary>
		public bool IsReady;

		/// <summary>
		/// Default constructor.
		/// </summary>
		/// <param name="manga">Manga project to display.</param>
		public Reader(Manga manga)
		{
			InitializeComponent();

			this.manga = manga;
			int volume = 0;
			//int chapter = 0;
			int width = -1, height = -1;
			var images = new List<Image>();

			if ((manga.ProjectFolder.Subfolders.Count != 1) && (manga.ProjectFolder.Subfolders.Count != 0))
			{
				var openVolDlg = new OpenVolume(manga);
				var res = openVolDlg.ShowDialog();
				if (res.HasValue != true)
					return;
				IsReady = res.Value;
				if (res.Value == false)
					return;
				volume = openVolDlg.volComboBox.SelectedIndex;
				//chapter = openVolDlg.chapComboBox.SelectedIndex; //yet not used!
				images = manga.GetVolumeFiles(volume);
			}
			else
			{
				IsReady = true;
				images = manga.GetVolumeFiles();
			}


			// Here we calculate maximum page width and height to make our reader have proper size.
			int counter = 0;
			foreach (var image in images)
			{
				var bp = new WPFMitsuControls.BookPage();
				bp.Content = image;
				if (counter % 2 == 0)
				{
					bp.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
				}
				else
				{
					bp.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
				}
				//readerBook.Items.Add(image);
				readerBook.Items.Add(bp);

				if (image.Source == null)
					continue;
				var b = image.Source as BitmapImage;
				if (b != null)
				{
					if (width < b.PixelWidth)
						width = b.PixelWidth;
					if (height < b.PixelHeight)
						height = b.PixelHeight;
				}
				else
				{
					var b1 = image.Source as CroppedBitmap;
					if (b1 != null)
					{
						if (width < b1.PixelWidth)
							width = b1.PixelWidth;
						if (height < b1.PixelHeight)
							height = b1.PixelHeight;
					}
				}

				counter++;
			}
			readerBook.Height = height;
			if (width > height) //if we met biggest page in landscape orientation...
				readerBook.Width = width;
			else readerBook.Width = width * 2; //...else lets make it twice as portrait page.
		}

		private void prevPageButton_Click(object sender, RoutedEventArgs e)
		{
			readerBook.AnimateToPreviousPage(false, 250);

			if ((Environment.OSVersion.Version.Major > 6) || (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor >= 1))
				TaskbarManager.Instance.SetProgressValue(readerBook.CurrentSheetIndex + 1, readerBook.Items.Count / 2);
		}

		private void nextPageButton_Click(object sender, RoutedEventArgs e)
		{
			readerBook.AnimateToNextPage(false, 250);

			if ((Environment.OSVersion.Version.Major > 6) || (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor >= 1))
				TaskbarManager.Instance.SetProgressValue(readerBook.CurrentSheetIndex + 1, readerBook.Items.Count / 2);
		}

		private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
		{
			switch (e.Key)
			{
				case System.Windows.Input.Key.Right:
					readerBook.AnimateToNextPage(false, 200);
					if ((Environment.OSVersion.Version.Major > 6) || (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor >= 1))
						TaskbarManager.Instance.SetProgressValue(readerBook.CurrentSheetIndex + 1, readerBook.Items.Count / 2);
					break;
				case System.Windows.Input.Key.Left:
					readerBook.AnimateToPreviousPage(false, 200);
					if ((Environment.OSVersion.Version.Major > 6) || (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor >= 1))
						TaskbarManager.Instance.SetProgressValue(readerBook.CurrentSheetIndex + 1, readerBook.Items.Count / 2);
					break;
				case System.Windows.Input.Key.Home:
					break;
				case System.Windows.Input.Key.End:
					break;
				case System.Windows.Input.Key.Space:
					//MessageBox.Show(readerBook.CurrentSheetIndex.ToString() + " " + readerBook.Items.Count.ToString());
					break;
				default:
					break;
			}
		}
	}
}
